@extends('admin/layout/master')
@section('title', '用户添加')
@section('title2', '用户添加')

@section('action')
  <a class="layui-btn layui-btn-small layui-btn-normal usersAdd_btn" href="{{ url('admin/user/index') }}"><i class="layui-icon"></i>用户列表</a>
	<!-- <a class="layui-btn layui-btn-small layui-btn-danger batchDel"><i class="layui-icon"></i>导出数据</a> -->
@endsection

@section('content')
<div class="error">
    @if (count($errors) > 0)
        <div class="alert alert-danger">
            错误列表：
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif
</div>

<form class="layui-form" name="mainform" action="{{ url('/admin/user/store') }}" method="post">
  <div class="layui-form-item">
    <label class="layui-form-label">昵称</label>
    <div class="layui-input-block">
      <input type="text" name="nickname" placeholder="请输入昵称" value="{{ old('nickname') }}" autocomplete="off" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">用户名</label>
    <div class="layui-input-block">
      <input type="text" name="username" required  lay-verify="required" placeholder="请输入用户名" value="{{ old('username') }}" autocomplete="off" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">密码</label>
    <div class="layui-input-inline">
      <input type="text" name="password" placeholder="请输入密码" autocomplete="off" value="{{ old('password') }}" class="layui-input">
    </div>
    <div class="layui-form-mid layui-word-aux">辅助文字</div>
  </div>
  <div class="layui-form-item">
    <div class="layui-input-block">
      {{ csrf_field() }}
      <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
      <!-- <a href="javascript:;" class="layui-btn" onclick="ajaxAdd()">立即提交</a> -->
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
</form>


<script>
function ajaxAdd()
{
        console.log($("[name='mainform']").serialize());
  $.ajax({
      url:"{{ url('/admin/user/store') }}",
      type:"post",
      dataType:"json",
      data: $("[name='mainform']").serialize(),
      success:function(re)
      {
        console.log(re);
          // if (re.code == "1") {
          //     layer.msg(re.msg, {icon: 1});
          //     setTimeout("parent.layer.close(parent.layer.getFrameIndex(window.name));window.parent.location.reload();",1000);
          // } else {
          //     layer.msg(re.msg, {icon: 2});
          // }
      },
      error:function(re)
      {
        // console.log(re);
        var msg = JSON.parse(re.responseText);
        var str = "";
        var num = 0;
        msg = msg.errors;
        console.log(msg.length);
        $.each(msg, function (k, v) {
          num++;
          if (num > 1) {
            str += "<br>" + v[0];
          } else {
            str += v[0];
          }
        });
        layer.alert(str);
      }
  })
}
</script>
@endsection

@section('layuijs')
<script>
//一般直接写在一个js文件中
layui.use(['layer', 'form'], function(){
var layer = layui.layer
,form = layui.form;
});
</script>
@endsection